package com.financing.wap.web.controller.api;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.stereotype.Controller;

import com.financing.wap.service.IFpFundCompanyService;
import com.financing.wap.service.IFpFundInfoService;
import com.financing.wap.service.IFpFundManagerService;
import com.financing.wap.common.Pager;
import com.financing.wap.common.ResultMap;
import com.financing.wap.common.ResultMap.STATUS;
import com.financing.wap.util.StringUtil;
import com.financing.wap.web.controller.BaseController;
import com.financing.wap.dto.FpFundCompanyDTO;
import com.financing.wap.dto.FpFundInfoDTO;
import com.financing.wap.dto.FpFundManagerDTO;

/**
 * @Description: Controller
 * @author caojiayao
 * @version 1.0
 * @created 
 */
@Controller
@RequestMapping("/api/fund/")
@Scope("prototype")
public class FpFundManagerAction  extends BaseController {
	@Autowired
	private IFpFundManagerService fpFundManagerService;
	
	@Autowired
	private IFpFundInfoService fpFundInfoService;
	
	@Autowired
	private IFpFundCompanyService fpFundCompanyService;
	
	@RequestMapping("/fdManagerQueryList")
	@ResponseBody
	public ResultMap queryList(HttpServletRequest request,
			HttpServletResponse response,FpFundManagerDTO dto,Pager pager) {
		ResultMap resultMap = new ResultMap();
		try{
			log.info("Begin query fa user list!");
			
			dto.setBegin(pager.getStartIndex());
			dto.setPageSize(pager.getPageSize());
			
			int count = fpFundManagerService.countFpFundManager(dto);
			pager.setTotalCount(count);
			List<FpFundManagerDTO> list = fpFundManagerService.listFpFundManager(dto);
			
			resultMap.setAttribute("list",list);
			resultMap.setAttribute("pager", pager);
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			
			log.info("End querylist!");
		}catch(Exception e){
			log.error("queryList error !",e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
			
			
			return resultMap;
	}
	
	@RequestMapping("/fdManagerDetail")
	@ResponseBody
	public ResultMap detail(HttpServletRequest request,
			HttpServletResponse response, FpFundManagerDTO requestDto) {
		ResultMap resultMap = new ResultMap();
		try{
			FpFundManagerDTO dto = fpFundManagerService.getFpFundManager(requestDto);
			
			//曾经任职的公司
			List<FpFundCompanyDTO> beforeCompanyList = new ArrayList<FpFundCompanyDTO>();
			//现在任职的公司
			FpFundCompanyDTO nowCompany = new FpFundCompanyDTO();
			//管理的基金
			List<FpFundInfoDTO> fundList = new ArrayList<FpFundInfoDTO>();
			if(dto != null && !StringUtil.isNullOrEmptyStr(dto.getJjjlbm())){
				beforeCompanyList = fpFundCompanyService.listFpFundCompanyByManagerId(dto.getJjjlbm());
				List<FpFundCompanyDTO> companyDTOs = fpFundCompanyService.getFpFundCompanyByManagerId(dto.getJjjlbm());
				if(companyDTOs != null && !companyDTOs.isEmpty()){
					nowCompany = companyDTOs.get(0);
				}
				fundList = fpFundInfoService.listFpFundInfoByManagerId(dto.getJjjlbm());
			}
			
			
			resultMap.setAttribute("detail", dto);
			resultMap.setAttribute("nowCompany", nowCompany);
			resultMap.setAttribute("beforeCompanyList", beforeCompanyList);
			resultMap.setAttribute("fundList", fundList);
			
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
		}catch(Exception e){
			log.error("query friendshipLink list error",e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return resultMap;
	}
	
}